$(function () {
    $("#welcome").load("phy-register.html");
    // $("#show-add-result").load("phy-examination.html");
    $("#show-add-result").css("display", "none");
    $("#div-manage-stu").css("display", "none");

    //刷新录入结果页面
    function ajaxAddResutl() {
        $.ajax({
            url: "/regStu",
            type: "GET",
            dataType: "json",
            contentType: "application/json;charset:utf-8",
            success: function (data) {
                var $stuDiv = $("#add-result");
                $stuDiv.empty();
                if (data.length == 0) {
                    alert("结果已全部录入。");
                }
                $.each(data, function (index, ele) {
                    var $item = $(" <div class=\"layui-col-md2\">\n" +
                        "            <div class=\"layui-card\">\n" +
                        "                <div class=\"layui-card-header\">待体检</div>\n" +
                        "                <div class=\"layui-card-body\">\n" +
                        "                    <span class=\"deptName\" style=\"width: 100%;\">" + ele.deptName + "</span></br></hr>\n" +
                        "                    <span class=\"stuName\">" + ele.stuName + "</span></br></hr>\n" +
                        "                    <span class=\"isPhy\">" + ele.isPhy + "</span>\n" +
                        "                    <span class=\"deptId\" style=\"display: none\">" + ele.deptId + "</span>\n" +
                        "                    <span class=\"stuId\" style=\"display: none\">" + ele.stuId + "</span>\n" +
                        "                    <button id=\"add\" class=\"layui-btn layui-btn-sm phyexam\" class=\"layui-btn\" style=\"margin-left: 25px;\">录入结果</button>\n" +
                        "                </div>\n" +
                        "            </div>\n" +
                        "        </div>");
                    if (ele.isPhy == "未体检") {
                        $stuDiv.append($item);
                    }
                });
            },
            error: function () {
                alert("error");
            }
        });
    }

    //开始体检按钮，添加学院界面出现，添加成功后返回原页面。
    $("#new-dept").on("click", function () {
        layui.use('layer', function () {
            var layer = layui.layer;
            layer.open({
                type: 1,
                title: "新增体检学院",
                skin: 'layui-layer-rim', //加上边框
                area: ['350px', '150px'], //宽高
                content: $("#new-dept-select")
            })
        });
    });
    // $("#deptSelect").append("<option value=''>请选择体检学院</option>");
    $.ajax({
        url: "/nonProDept",
        type: "GET",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        success: function (data) {
            $.each(data, function (index, ele) {
                // var $item = $("<option value = '" + ele.deptId + "'>" + ele.deptName + "</option>");
                $("#deptSelect").append("<option value = '" + ele.deptId + "'>" + ele.deptName + "</option>");
            });
        },
        error: function () {
            alert("error");
        }
    });
    $("#btnAddPhyDept").click(function () {
        var deptId = $("#deptSelect option:selected").val();
        $.ajax({
            url: "/pro2stu",
            type: "POST",
            contentType: "application/json;charset=utf-8",
            // dataType: "json",
            data: JSON.stringify({
                "deptId": deptId
            }),
            success: function (data) {
                alert("学院添加成功，等待体检后录入结果！");
                $("#welcome").css("display", "none");
                $("#table-result").css("display", "none");
                $("#div-manage-stu").css("display", "none");
                $("#show-add-result").css("display", "block");
                ajaxAddResutl();
                $("#temp").css("display", "none");
            },
            error: function (XMLHttpRequest, textStatus) {
                console.log(XMLHttpRequest.status);
                console.log(XMLHttpRequest.readyState);
                console.log(textStatus);
            }
        });
    });

    //添加体检结果按钮，显示体检人员信息界面，
    $("#btn-add-result").click(function () {
        ajaxAddResutl();
        $("#welcome").css("display", "none");
        $("#table-result").css("display", "none");
        $("#temp").css("display", "none");
        $("#show-add-result").css("display", "block");
    });

    function tableResult() {
        layui.use('table', function () {
            var table = layui.table;
            table.render({
                elem: '#result'
                , url: '/allResult'
                , cellMinWidth: 80 //全局定义常规单元格的最小宽度，layui 2.2.1 新增
                , limit: 5
                , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']//自定义分页布局
                    , limits: [5, 10, 15]
                    , first: false //不显示首页
                    , last: false //不显示尾页
                }
                // , toolbar: 'default'
                , cols: [[
                    {type: 'checkbox', fixed: 'left'}
                    // , {field: 'id', title: 'ID', sort: true}
                    , {field: 'num', title: '学号', sort: true}
                    , {field: 'stuName', title: '姓名'} //width 支持：数字、百分比和不填写。你还可以通过 minWidth 参数局部定义当前单元格的最小宽度，layui 2.2.1 新增
                    , {field: 'age', title: '年龄', sort: true}
                    , {field: 'sex', title: '性别'}
                    , {field: 'contact', title: '联系方式'}
                    , {field: 'deptName', title: '所属院系'}
                    , {field: 'proName', title: '项目'}
                    , {field: 'result', title: '结果', edit:'text'}
                    // , {field: 'qualify', title: '是否合格', align: 'center'}
                    // , {field: 'classify', title: '职业', align: 'center'} //单元格内容水平居中
                    // , {field: 'experience', title: '积分', sort: true, align: 'right'} //单元格内容水平居中
                    // , {field: 'score', title: '评分', sort: true, align: 'right'}
                    , {field: 'remark', title: '备注', align: 'center', width: 150}
                    , {fixed: 'right', title: '操作', toolbar: '#editBar', width: 150}
                ]],
                id: 'result'
            });
            var $ = layui.$, active = {
                reload: function () {
                    //获取用户名
                    // var demoReload = $('#stu_search_name');
                    //执行重载
                    table.reload('result', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        //根据条件查询
                        , where: {
                            // stuName: demoReload.val()
                            key: {
                                stuName: $("#stu_search_name").val()
                            }
                        }
                    });
                }
            };
            //点击搜索按钮根据用户名称查询
            $('#search-stu').on('click',
                function () {
                    // var type = $(this).data('type');
                    // active[type] ? active[type].call(this) : '';
                    active["reload"] ? active["reload"].call(this) : '';
                });
        });
    }

    function displayResult() {
        $.ajax({
            url: "/allResult",
            type: "GET",
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (data) {

            }

        })
    }

    //查看体检信息按钮，显示体检结果信息界面，
    $("#show-result").click(function () {
        tableResult();
        $("#welcome").css("display", "none");
        $("#show-add-result").css("display", "none");
        $("#temp").css("display", "none");
        $("#div-manage-stu").css("display", "none");
        $("#table-result").css("display", "block");
    });

    //返回按钮
    $("#back").click(function () {
        $("#table-result").css("display", "none");
        $("#show-add-result").css("display", "none");
        $("#div-manage-stu").css("display", "none");
        $("#welcome").css("display", "block");
        $("#temp").css("display", "block");
    });


    //学院管理
    $("#manage-stu").click(function () {
        // alert("123")
        $("#table-result").css("display", "none");
        $("#show-add-result").css("display", "none");
        $("#welcome").css("display", "none");
        $("#temp").css("display", "none");
        $("#div-manage-stu").load("manage-stu.html");
        $("#div-manage-stu").css("display", "block");
    })

});